package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.impl.NavImpl;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.NavService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NavServiceImpl implements NavService {
    @Override
    public List<NavModel> getNavList() {
        List<NavModel> list = new ArrayList<>();
        NavDao navDao = new NavImpl();
        Jedis jedis = new Jedis();
        if (jedis.exists("navHref")) {
            List<String> navHref = jedis.lrange("navHref", 0, -1);
            List<String> navTitle = jedis.lrange("navTitle", 0, -1);
            for (int i = 0; i < navHref.size(); i++) {
                NavModel model = new NavModel();
                model.setHref(navHref.get(i));
                model.setTitle(navTitle.get(i));
                list.add(model);
            }
        } else {
            list = navDao.getNavList();
            for (NavModel model : list
            ) {
                jedis.rpush("navHref", model.getHref());
                jedis.rpush("navTitle", model.getTitle());

            }

        }
        return list;
    }
    public String add(NavModel model){
        NavDao navDao=new NavImpl();
        int result=navDao.add(model);
        if(result>0){
            Jedis jedis=new Jedis();
            jedis.del("navHref","navTitle");
        }
        return  result>0?"success":"error";
    }
}
